import time

import allure
from selenium.webdriver.common.by import By

from Base.BackBase.baseBack import BasePage, BaseHandle

# 后台审核文章对象库层
from utils import choose_channel


class AuditPage(BasePage):
    def __init__(self):
        super().__init__()
        # 文章名称
        self.title = By.CSS_SELECTOR, "[placeholder='请输入: 文章名称']"
        # 状态
        self.status = By.CSS_SELECTOR, "[placeholder='请选择状态']"
        # 时间
        self.end_time = By.CSS_SELECTOR, "[placeholder='选择结束时间']"
        # 查询
        self.select_button = By.CSS_SELECTOR, ".find"
        # 通过
        self.pass_button = By.XPATH, "//div[@class='cell']/button[2]"
        # 确定
        self.confirm_button = By.XPATH, "//div[@class='el-message-box__btns']/button[2]/span"
        #     发表过的标题
        self.used_title = By.XPATH, "//tr[1]/td[2]/div"

    # 定位文章名称
    def find_title(self):
        return self.get_element(self.title)

    #     定位状态
    def find_status(self):
        return self.get_element(self.status)

    # 定位时间
    def find_end_time(self):
        return self.get_element(self.end_time)

    # 定位查询
    def find_select_button(self):
        return self.get_element(self.select_button)

    # 定位通过
    def find_pass_button(self):
        return self.get_element(self.pass_button)

    # 定位确定
    def find_confirm_button(self):
        return self.get_element(self.confirm_button)

    #     定位发表过的标题
    def find_used_title(self):
        return self.get_element(self.used_title)


# 后台审核文章操作层
class AuditHandle(BaseHandle):
    def __init__(self):
        self.audit_page = AuditPage()

    # 输入文章标题
    @allure.step(title="输入文章标题")
    def send_title(self, title):
        self.input_text(self.audit_page.find_title(), title)

    # 选择状态
    @allure.step(title="选择状态")
    def click_status(self, channel):
        choose_channel(self.audit_page.back_driver, self.audit_page.find_status(), channel)

    # 输入时间
    @allure.step(title="输入时间")
    def send_end_time(self, end_time):
        self.input_text(self.audit_page.find_end_time(), end_time)

    # 点击查询
    @allure.step(title="点击查询")
    def click_select_button(self):
        self.audit_page.find_select_button().click()

    # 点击通过
    @allure.step(title="点击通过")
    def click_pass_button(self):
        self.audit_page.find_pass_button().click()

    # 点击确定
    @allure.step(title="点击确定")
    def click_confirm_button(self):
        self.audit_page.find_confirm_button().click()

    #      获取发表过的文章标题
    @allure.step(title="获取发表过的文章标题")
    def get_used_title(self):
        return self.audit_page.find_used_title().text


# 后台审核文章业务层
class AuditProxy:
    def __init__(self):
        self.audit_handle = AuditHandle()

    #     审核发表文章
    def audit_article(self, title, channel, end_time):
        self.audit_handle.send_title(title)
        self.audit_handle.click_status(channel)
        self.audit_handle.send_end_time(end_time)
        self.audit_handle.click_select_button()
        time.sleep(3)
        self.audit_handle.click_pass_button()
        self.audit_handle.click_confirm_button()

    # 查看发表文章是否成功
    def success_audit_article(self, title):
        self.audit_handle.send_title(title)
        self.audit_handle.click_status("审核通过")
        self.audit_handle.click_select_button()
        time.sleep(3)
        return self.audit_handle.get_used_title()
